

#ifndef __PAT_ARRAY_H
#define __PAT_ARRAY_H

#include "sentence.h"
#include "suffix_tree.h"
#include "dictionary/dict.h"

extern double FACTOR;
extern const double MINMUM_ENTROPY;
extern char output_dir[128];

struct LcpData
{
  // store a index pointing to ContextData stroing
  // original string.
  int index;

  double rightEntropy;
  double leftEntropy;
  double totalEntropy;

  int crossingTimes;
  int notCrossingTimes;
  int wordFragment;

  bool isInDict;
  bool filtered;
  bool asymmetricEntropy;

  bool isCrossingBorder()
  {
	  return crossingTimes>notCrossingTimes;
  }
	  
  bool isEntropyBeyondFactor()
  {
	  return leftEntropy>FACTOR && rightEntropy>FACTOR;
  }

  LcpData()
  {
	  crossingTimes = notCrossingTimes=wordFragment=0;	
	  totalEntropy=rightEntropy=leftEntropy=0.0;

	  isInDict =false;
	  filtered=false;
		asymmetricEntropy=false;
  }

  bool allNewWordst()
  {
	  return !isCrossingBorder() &&isEntropyBeyondFactor();
  }

  bool notInDictNew()
  {
	  return !isCrossingBorder() && !isInDict && isEntropyBeyondFactor();
  }
};

class PatArray
{

private:
	static wchar_t *key;

private:
	Dictionary *dict;

	int ctxSize;

	SuffixTree * rst;
	WordContext * rctx;
	
	SuffixTree * lst;
	WordContext * lctx;
	
	LcpData * lcp;

	double candidateWordFactor;
	LcpData **orderedLcp;
	int candidateNewWordLength;

public:
	PatArray();
	PatArray(Dictionary *dict);
	~PatArray();

	void add(Sentence * sentence);
	bool generateNewWord();

	void calculateEntropy();
	

	void printLcpData();
	void printBoundaryCounterInfo();
	void printContext();
	void printPrefixList();

	void printEntropyInOrder();
private:
	
	void init();
	void generateWordContext();

	void calculateCrossingWordBoundary();

	void filterCharacter();
	void isInDictFilter();
	void isAsymmetricEntropy();
	
	static int compareWordContext( const void * w1, const void * w2);
	static int compareLcpDataByEntropy(const void *v1, const void *v2); 
};

//--------------------------------------
void printWordContextNode(WordContext* rctx,int size);

#endif